package rs.networx.servlets;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import rs.networx.dao.NottificationDao;
import rs.networx.entity.Nottification;
import rs.networx.entity.Person;
import rs.networx.util.ErrorMessages;

public class ViewNottificationsLoggedUserServlet extends HttpServlet {

	private static final long serialVersionUID = -6324271562909561938L;

	@EJB
	private NottificationDao nottificationDao;
	
	private String username;
	private String password;
	private Integer intLoggedUserID;
	private Person loggedUser;
	
	private Integer nottificationsCount;
	
	private List<Nottification> nottifications;
		public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		
		System.out.println("LOADING: ViewNottificationsLoggedUserServlet");
		
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding(response.getCharacterEncoding());
		
		HttpSession session = request.getSession();
		loggedUser = (Person)session.getAttribute("logovaniUser");
		
		try {	
			username = loggedUser.getEmail();
			password = loggedUser.getPassword();
			intLoggedUserID = loggedUser.getId();
			System.out.println(username + "  " + password + "  " + intLoggedUserID);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		
		if(loggedUser != null){
			nottifications = nottificationDao.getNonViewedNottForUser(intLoggedUserID);
			System.out.println("nottifications: " + nottifications.size());
			
			if(nottifications != null && nottifications.size() > 0){
				// sada dobavljamo broj nonviewed notifikacija (ako ih ima) za datog ulogovanog korisnika...
				nottificationsCount = nottificationDao.getNottificationsCountForLoggedUser(loggedUser);
				System.out.println("nottificationsCount: " + nottificationsCount);
				
				// nottificationsCount bi trebao da bude isti kao i nottifications.size()...
				System.out.println("nottificationsCount: " + nottificationsCount + "  " + "nottifications: " + nottifications.size());
				
				// zakomentarisani kod prebacen u GoBackToRegistredUserHomePageServlet...
				/*// kada smo dobavili sve nonviewed notifikacije za logged usera, njih stavljamo da su viewed...
				// i redirektujemo na ViewNottificationsLoggedUser.jsp...
				for(int i = 0; i < nottifications.size(); i ++){
					nottifications.get(i).setViewed(true);
					nottificationDao.merge(nottifications.get(i));
				}*/
							
				session.setAttribute("searchStatus", ErrorMessages.PERSON_HAVE_NOTTIFICATIONS);
				session.setAttribute("nottifications", nottifications);
				session.setAttribute("nottificationsCount", nottificationsCount);
			}else if(nottifications == null || nottifications.size() == 0){
				session.setAttribute("searchStatus", ErrorMessages.PERSON_DO_NOT_HAVE_NOTTIFICATIONS);
			}
			request.getRequestDispatcher("ViewNotifications.jsp").forward(request, response);
			return;
		}else if(loggedUser == null){
			request.getRequestDispatcher("Welcome.jsp").forward(request, response);
			return;
		}
	}
	
	//--------------------------------------------------------------------------------------------------------------------------------------------------
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		doGet(request, response);
	}
	
	
}
